library(survey)
library(foreign)
library(ggplot2)
library(cowplot)
library(dplyr)
library(egg)
library(ggpubr)
library(gridGraphics)

## see wave2_CLEANING.R for data cleaning
load("~/Dropbox/Public_Conf_Mil/Data_and_code/wave2_data/clean_dataw2.RData")

## Create weighted survey design object
w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df
  )

table(df$DOV_INST)
table(df$DOV_PROF)

svyby(~ Q7_d, ~ party+DOV_INST, w2_design, svymean, na.rm = TRUE)
svyby(~ Q9_d, ~ party+DOV_PROF, w2_design, svymean, na.rm = TRUE)

svyby(~ Q8A_r, ~ party+DOV_INST, w2_design, svymean, na.rm = TRUE)

in_part <- svyby(~ Q8A_r, ~ DOV_INST, w2_design, svymean, na.rm = TRUE)
in_good <- svyby(~ Q8B_r, ~ DOV_INST, w2_design, svymean, na.rm = TRUE)
in_repr <- svyby(~ Q8C_r, ~ DOV_INST, w2_design, svymean, na.rm = TRUE)
in_ethi <- svyby(~ Q8D_r, ~ DOV_INST, w2_design, svymean, na.rm = TRUE)
in_true <- svyby(~ Q8E_r, ~ DOV_INST, w2_design, svymean, na.rm = TRUE)
in_valu <- svyby(~ Q8F_r, ~ DOV_INST, w2_design, svymean, na.rm = TRUE)
in_most <- svyby(~ Q8G_r, ~ DOV_INST, w2_design, svymean, na.rm = TRUE)
in_know <- svyby(~ Q8H_r, ~ DOV_INST, w2_design, svymean, na.rm = TRUE)

ml_part <- svymean(~ Q13A_r, w2_design, na.rm = TRUE)
ml_good <- svymean(~ Q13B_r, w2_design, na.rm = TRUE)
ml_repr <- svymean(~ Q13C_r, w2_design, na.rm = TRUE)
ml_ethi <- svymean(~ Q13D_r, w2_design, na.rm = TRUE)
ml_true <- svymean(~ Q13E_r, w2_design, na.rm = TRUE)
ml_valu <- svymean(~ Q13F_r, w2_design, na.rm = TRUE)
ml_most <- svymean(~ Q13G_r, w2_design, na.rm = TRUE)
ml_know <- svymean(~ Q13H_r, w2_design, na.rm = TRUE)

#### FIGURE 6.5 - INSTITUTION ATTRIBUTES, BAR CHART ####

atts_df <- data.frame(part = c(ml_part[1]*100, in_part[,2]*100),
                      good = c(ml_good[1]*100, in_good[,2]*100),
                      repr = c(ml_repr[1]*100, in_repr[,2]*100),
                      ethi = c(ml_ethi[1]*100, in_ethi[,2]*100),
                      true = c(ml_true[1]*100, in_true[,2]*100),
                      valu = c(ml_valu[1]*100, in_valu[,2]*100),
                      know = c(ml_know[1]*100, in_know[,2]*100),
                      insts = c(1:5))

pa_plot <- ggplot(atts_df) +
  geom_col(aes(x = insts, y = part), fill = "gray60", width = 0.5) +
  scale_x_continuous(breaks = c(1,2,3,4,5),
                     labels = c("Military", "CDC", "State Dept", "IRS", "USPS")) +
  ggtitle("Too Partisan") +
  xlab("") + ylab("") + ylim(0,100) + theme_bw()
pa_plot

go_plot <- ggplot(atts_df) +
  geom_col(aes(x = insts, y = good), fill = "gray60", width = 0.5) +
  scale_x_continuous(breaks = c(1,2,3,4,5),
                     labels = c("Military", "CDC", "State Dept", "IRS", "USPS")) +
  ggtitle("Good at What It Does") +
  xlab("") + ylab("") + ylim(0,100) + theme_bw()
go_plot

re_plot <- ggplot(atts_df) +
  geom_col(aes(x = insts, y = repr), fill = "gray60", width = 0.5) +
  scale_x_continuous(breaks = c(1,2,3,4,5),
                     labels = c("Military", "CDC", "State Dept", "IRS", "USPS")) +
  ggtitle("Representative of U.S.") +
  xlab("") + ylab("") + ylim(0,100) + theme_bw()
re_plot

et_plot <- ggplot(atts_df) +
  geom_col(aes(x = insts, y = ethi), fill = "gray60", width = 0.5) +
  scale_x_continuous(breaks = c(1,2,3,4,5),
                     labels = c("Military", "CDC", "State Dept", "IRS", "USPS")) +
  ggtitle("Ethical") +
  xlab("") + ylab("") + ylim(0,100) + theme_bw()
et_plot

tc_plot <- ggplot(atts_df) +
  geom_col(aes(x = insts, y = true), fill = "gray60", width = 0.5) +
  scale_x_continuous(breaks = c(1,2,3,4,5),
                     labels = c("Military", "CDC", "State Dept", "IRS", "USPS")) +
  ggtitle("Makes Truthful Claims") +
  xlab("") + ylab("") + ylim(0,100) + theme_bw()
tc_plot

sv_plot <- ggplot(atts_df) +
  geom_col(aes(x = insts, y = valu), fill = "gray60", width = 0.5) +
  scale_x_continuous(breaks = c(1,2,3,4,5),
                     labels = c("Military", "CDC", "State Dept", "IRS", "USPS")) +
  ggtitle("Shares My Values") +
  xlab("") + ylab("") + ylim(0,100) + theme_bw()
sv_plot

kn_plot <- ggplot(atts_df) +
  geom_col(aes(x = insts, y = know), fill = "gray60", width = 0.5) +
  scale_x_continuous(breaks = c(1,2,3,4,5),
                     labels = c("Military", "CDC", "State Dept", "IRS", "USPS")) +
  ggtitle("Knowledgeable about Job") +
  xlab("") + ylab("") + ylim(0,100) + theme_bw()
kn_plot

fig65_h <- ggarrange(pa_plot + theme(axis.text.x = element_text(size = 6),
                                     axis.text.y = element_text(size = 6),
                                     plot.title = element_text(size = 10)), 
                     go_plot + theme(axis.text.x = element_text(size = 6),
                                     axis.text.y = element_text(size = 6),
                                     plot.title = element_text(size = 10)),
                     re_plot + theme(axis.text.x = element_text(size = 6),
                                     axis.text.y = element_text(size = 6),
                                     plot.title = element_text(size = 10)), 
                     et_plot + theme(axis.text.x = element_text(size = 6),
                                     axis.text.y = element_text(size = 6),
                                     plot.title = element_text(size = 10)), 
                     tc_plot + theme(axis.text.x = element_text(size = 6),
                                     axis.text.y = element_text(size = 6),
                                     plot.title = element_text(size = 10)), 
                     sv_plot + theme(axis.text.x = element_text(size = 6),
                                     axis.text.y = element_text(size = 6),
                                     plot.title = element_text(size = 10)),
                     kn_plot + theme(axis.text.x = element_text(size = 6),
                                     axis.text.y = element_text(size = 6),
                                     plot.title = element_text(size = 10)),
                     nrow = 2, ncol = 4)
fig65_h

fig65_v <- ggarrange(pa_plot + theme(axis.text.x = element_text(size = 6),
                                     axis.text.y = element_text(size = 6),
                                     plot.title = element_text(size = 10)), 
                     go_plot + theme(axis.text.x = element_text(size = 6),
                                     axis.text.y = element_text(size = 6),
                                     plot.title = element_text(size = 10)),
                     re_plot + theme(axis.text.x = element_text(size = 6),
                                     axis.text.y = element_text(size = 6),
                                     plot.title = element_text(size = 10)), 
                     et_plot + theme(axis.text.x = element_text(size = 6),
                                     axis.text.y = element_text(size = 6),
                                     plot.title = element_text(size = 10)), 
                     tc_plot + theme(axis.text.x = element_text(size = 6),
                                     axis.text.y = element_text(size = 6),
                                     plot.title = element_text(size = 10)), 
                     sv_plot + theme(axis.text.x = element_text(size = 6),
                                     axis.text.y = element_text(size = 6),
                                     plot.title = element_text(size = 10)),
                     kn_plot + theme(axis.text.x = element_text(size = 6),
                                     axis.text.y = element_text(size = 6),
                                     plot.title = element_text(size = 10)),
                     nrow = 4, ncol = 2)
fig65_v


#### FIGURE 6.6 - OTHERS CONFIDENCE ####

ot_df <- data.frame(ot = c(ml_most[1]*100, in_most[,2]*100),
                    insts = c(1:5))

ot_plot <- ggplot(ot_df, aes(x = insts, y = ot)) +
  geom_col(aes(x = insts, y = ot), fill = "gray60", width = 0.5) +
  scale_x_continuous(breaks = c(1,2,3,4,5),
                     labels = c("Military", "CDC", "State Dept", "IRS", "USPS")) +
  geom_text(label = sprintf("%0.1f", ot_df$ot),
            vjust = -0.2) +
  xlab("") + ylab("Respondents Who Agree (%)") + ylim(0,80) + theme_bw()
ot_plot
ggsave("~/Dropbox/Public_Conf_Mil/Chapter 6 Figures/fig6-6_others.jpeg", 
       plot = ot_plot, dpi = 1000)

#### FIGURE 6.7 - PCA ####

in_mil_pca <- svyprcomp(~ Q8A_r + Q8B_r + Q8C_r + Q8D_r + Q8E_r + Q8F_r + Q8H_r + 
                          Q13A_r + Q13B_r + Q13C_r + Q13D_r + Q13E_r + Q13F_r + 
                          Q13H_r, w2_design, scale = TRUE)
insts_pca <- svyprcomp(~ Q8A_r + Q8B_r + Q8C_r + Q8D_r + Q8E_r + Q8F_r + Q8H_r, 
                       w2_design, scale = TRUE)

insts_pca$rotation[,2]

pca_df <- data.frame(comp1 = insts_pca$rotation[,1],
                     comp2 = insts_pca$rotation[,2],
                     text = c("Too Partisan", "Good at Job", "Representative", 
                              "Ethical", "Truthful", "Shares Values", "Knowledgeable"))

pca_plot <- ggplot(pca_df, aes(x = comp1, y = comp2, label = text)) +
  geom_point() + 
  geom_text(check_overlap = FALSE, 
            nudge_y = c(0.02, -0.025, -0.015, 0.05, 0.02, -0.014, -0.028),
            nudge_x = c(0.02, -0.018, -0.024, 0.01, -0.013, -0.024, -0.02)) +
  xlab("Component 1 (Affinity)") + ylab("Component 2 (Party)") +
  theme_bw()
pca_plot
ggsave("~/Dropbox/Public_Conf_Mil/Chapter 6 Figures/fig6-7_loadings.jpeg", 
       plot = pca_plot, dpi = 1000)
